/*
 *  Copyright 2011.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import org.opu.pc.ui.util.ResourceHandle;
import org.opu.pc.ui.util.ResourceHandle.IconType;

/**
 *
 */
public class Exit extends AbstractAction {
	
	private static final long serialVersionUID = -1394672822044816429L;
	
	/** My name */
    public static final String MNAME = "Exit";
    private Set<ActionListener> listeners ;

    public Exit() {
        this(new LinkedHashSet<ActionListener>());
    }

    public Exit(Set<ActionListener> listeners) {
        this.listeners = listeners;
    }

    public void addActionListeners(Collection<ActionListener> listeners){
        this.listeners.addAll(listeners);
    }

    public void addActionListener(ActionListener listener){
        this.listeners.add(listener);
    }

    public void removeActionListener(ActionListener listener){
        this.listeners.remove(listener);
    }

    @Override
    public void action(ActionEvent e) {
        String name = getName() + "." + "Text";
        logger.debug(this, "resource key - " + name);

        String yes = boundle.getString(name + ".yes");
        String no = boundle.getString(name + ".no");
        Object[] options = {yes, no};
        
        String appName = getVarValue();
        String msg = MessageFormat.format(boundle.getString(name), appName);
        logger.debug(this, "message: " + msg);

        String ttl = appName + " - " + getValue(SHORT_DESCRIPTION);
        
        int i = JOptionPane.showOptionDialog(mainView.asFrame(), msg, ttl,
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                ResourceHandle.getIcon("Exit.png", IconType.large), 
                options, options[0]);

        if (i == JOptionPane.YES_OPTION) {
            logger.debug(this, "approved");
            for (ActionListener a : listeners) {
                logger.info(this, "Call final action: " + a);
                try {
                    a.actionPerformed(e);
                } catch (Exception e1) {
                    logger.error(this, e1);
                }
            }

            logger.info(this, "Exit");
            doExit();
        }
        
        onActionFinish();
    }

    protected String getVarValue() {
        return ResourceHandle.getApplicationName();
    }

    protected void doExit() {
        System.exit(0);
    }


    @Override
    public String getName(){
        return MNAME;
    }
}
